package com.zc.figerstore;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.tencent.soter.wrapper.SoterWrapperApi;
import com.tencent.soter.wrapper.wrap_callback.SoterProcessAuthenticationResult;
import com.tencent.soter.wrapper.wrap_callback.SoterProcessCallback;
import com.tencent.soter.wrapper.wrap_callback.SoterProcessKeyPreparationResult;
import com.tencent.soter.wrapper.wrap_fingerprint.SoterFingerprintCanceller;
import com.tencent.soter.wrapper.wrap_fingerprint.SoterFingerprintStateCallback;
import com.tencent.soter.wrapper.wrap_task.AuthenticationParam;

public class MainActivity extends AppCompatActivity {

    private TextView text;
    private SoterFingerprintCanceller mCanceller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        text = (TextView) findViewById(R.id.text);
    }

    // 初始化数据
    private void initData() {
        SoterWrapperApi.prepareAuthKey(new SoterProcessCallback<SoterProcessKeyPreparationResult>() {
            @Override
            public void onResult(@NonNull SoterProcessKeyPreparationResult result) {
                Log.e("result1", result.toString());
            }
        }, false, true, 0, null, null);


        if (mCanceller != null) {
            mCanceller = null;
        }
        mCanceller = new SoterFingerprintCanceller();

        AuthenticationParam param = new AuthenticationParam.AuthenticationParamBuilder()
                .setScene(0)
                .setContext(MainActivity.this)
                .setFingerprintCanceller(new SoterFingerprintCanceller())
                .setPrefilledChallenge("test challenge")
                .setSoterFingerprintStateCallback(new SoterFingerprintStateCallback() {
                    @Override
                    public void onStartAuthentication() {
                        Log.e("finger", "onStartAuthentication");
                    }

                    @Override
                    public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                        Log.e("finger", "onAuthenticationHelp");
                    }

                    @Override
                    public void onAuthenticationSucceed() {
                        mCanceller = null;
                        Log.e("finger", "onAuthenticationSucceed");
                    }

                    @Override
                    public void onAuthenticationFailed() {
                        Log.e("finger", "onAuthenticationFailed");
                    }

                    @Override
                    public void onAuthenticationCancelled() {
                        mCanceller = null;
                        Log.e("finger", "onAuthenticationCancelled");
                    }

                    @Override
                    public void onAuthenticationError(int errorCode, CharSequence errorString) {
                        mCanceller = null;
                        Log.e("finger", "onAuthenticationError");
                    }
                }).build();
        SoterWrapperApi.requestAuthorizeAndSign(new SoterProcessCallback<SoterProcessAuthenticationResult>() {
            @Override
            public void onResult(@NonNull SoterProcessAuthenticationResult result) {
                Log.e("result2", result.toString());
            }
        }, param);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SoterWrapperApi.release();
    }
}
